<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>

<!-- 更新用户弹出层 -->
<div id="update-user-layer" style="display: none; padding: 20px">
    <form id="update-user-form" class="layui-form layui-form-pane" lay-filter="update-user-form">
        <div class="layui-form-item">
            <label class="layui-form-label">工号</label>
            <div class="layui-input-block">
                <input type="text" name="account" id="account" class="layui-input" readonly>
            </div>
        </div>

                <input type="hidden" name="password" id="password" class="layui-input">

        <div class="layui-form-item">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-block">
                <input type="text" name="name" id="name" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">角色</label>
            <div class="layui-input-block">
                <input type="radio" name="role"  value="0" title="管理员" checked="">
                <input type="radio" name="role"  value="1" title="超级管理员">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-block">
                <input type="text" name="sex" id="sex" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">联系电话</label>
            <div class="layui-input-block">
                <input type="text" name="phone_number" id="phone_number" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
                <input type="text" name="mailbox" id="mailbox" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="radio" name="state"  value="0" title="未锁定" checked="">
                <input type="radio" name="state"  value="1" title="已锁定">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="update-user-form-submit">提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>

<div class="layuimini-container">
    <div class="layuimini-main">

        <div class="demoTable">
            搜索姓名：
            <div class="layui-inline">
                <input class="layui-input" name="name" id="demoReload1" autocomplete="off">
            </div>
            <button class="layui-btn" data-type="reload">搜索</button>
        </div>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
            </div>
        </script>

        <table class="layui-hide" id="administrators" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            administrators = layui.table;

        administrators.render({
            elem: '#administrators',
            url:'/getAlladministrators',
            toolbar: '#toolbarDemo',
            title:'管理员信息',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {field: 'account', width: 120, title: '工号', sort: true},
                {field: 'name', width: 120, title: '姓名'},
                {field: 'role', width: 120, title: '角色',templet:function(res){
                    if(res.role==0)return "管理员"
                        else return "超级管理员"
                    }},
                {field: 'sex', title: '性别', minWidth: 120},
                {field: 'phone_number', width: 200, title: '联系电话'},
                {field: 'mailbox', width: 200, title: '邮箱'},
                {field: 'state', width: 120, title: '状态',templet:function(res){
                        if(res.state==0)return "未锁定"
                        else return "已锁定"
                    }},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            id: 'testReload',
        });
        var $ = layui.$, active = {
            reload: function() {

                var demoReload1 = $('#demoReload1');

                administrators.reload('testReload', {
                    url: '/getByName',
                    where: {
                        name: demoReload1.val()
                    }
                }, 'data');

            }
        };

        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        /**
         * toolbar监听事件
         */
        administrators.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加管理员',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '../page/table/addadministrators.html',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                administrators.reload('testReload');
            }
        });

        //监听表格复选框选择
        administrators.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        administrators.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                // 每次显示更新用户的表单前自动为表单填写该行的数据
                form.val('update-user-form', {
                    "account": data.account,
                    "password": data.password,
                    "name":data.name,
                    "role":data.role,
                    "sex":data.sex,
                    "phone_number":data.phone_number,
                    "mailbox":data.mailbox,
                    "state":data.state
                });
                // 显示更新用户表单的弹出层
                layer.open({
                    type: 1,
                    title: '编辑管理员',
                    skin: 'layui-layer-molv',
                    area: ['500px'],
                    content: $('#update-user-layer')
                });
                // 更新用户表单提交
                form.on('submit(update-user-form-submit)', function(data) {
                    var param=data.field;//定义临时变量获取表单提交过来的数据，非json格式
                    console.log(param);//测试是否获取到表单数据，调试模式下在页面控制台查看
                    // ajax方式更新用户
                    $.ajax({
                        url: "/modifyAdministrators",
                        type:"PUT",
                        data: JSON.stringify(data.field),
                        contentType: 'application/json;charset=utf-8',
                        dataType: 'json',
                        success: function(data) {
                            if (data.status == 1) {
                                layer.close(layer.index);
                                layer.msg('更新成功');
                                administrators.reload('testReload');
                            } else {
                                layer.msg('更新失败');
                            }
                        },
                        error: function() {
                            console.log("ajax error");
                        }
                    });
                    // 阻止表单跳转
                    return false;
                });

            } else if (obj.event === 'delete') {
                layer.confirm('确定删除该管理员吗', function (index) {
                    $.ajax({
                        url:"/deleteByAccount",
                        data:{"account":data.account},
                        success:function (data) {
                            if(data.status==1){
                                layer.msg("删除成功");
                                obj.del();
                            }else {
                                layer.msg("删除失败");
                            }

                        }
                    })
                    layer.close(index);
                });
            }
        });

    });
</script>

</body>
</html>